﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DataLib.Common;
using DataLib.Accounts.Bus;
using DataLib.Accounts.Data;
using DataLib.Model;
using DataLib.BLL;

namespace Ewts.Project
{
    public partial class AllocationLookSaleman : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] == null)
                {
                    Response.Redirect("/Accounts/Login.aspx");
                }
                DataLib.Model.ServerPoint modelPoint = new DataLib.Model.ServerPoint();
                if (Request["cityid"] != null && Request["cityid"].ToString().Trim() != "")
                {
                    int cityid = int.Parse(Request["cityid"].ToString());
                    DataLib.BLL.ServerPoint bllPoint = new DataLib.BLL.ServerPoint();
                    modelPoint = bllPoint.GetModel(cityid);
                    string cumid = new DataLib.BLL.Project().GetModel(
                    (int)new DataLib.BLL.ServerArea().GetModel((int)modelPoint.ServerAreaID).ProjectID).CustomerID.ToString();             
                    txtareaName.Text = modelPoint.AreaName;
                    txtserverpoint.Text = modelPoint.ServerPointName;
                    bindDropList(cumid);
                }
                getDetile();
                if (!string.IsNullOrEmpty(Request.QueryString["addressid"]))
                {
                    BindUpdateInfo();
                }
                else
                {
                    getGrid();
                }
            }
        }

        private void getDetile()
        {
            int id = 0;
            if (Request["id"] != null && Request["id"].ToString().Trim() != "")
            {
                id = int.Parse(Request["id"].ToString());
            }
            DataLib.Model.ServerPoint pModel = new DataLib.Model.ServerPoint();
            DataLib.BLL.ServerPoint pBll = new DataLib.BLL.ServerPoint();
            pModel = pBll.GetModel(id);
            if(pModel!= null)
            {
                this.txtareaName.Text = pModel.AreaName;
                this.txtserverpoint.Text = pModel.ServerPointName.ToString().Replace('@',' ');
            }
            
        }

        private void bindDropList(string cumid)
        {
            string strWhere = " customerid=" + cumid + " and id not in (select addressid from projectsrelation where projectid= " + cumid + ")";
            DataLib.BLL.CumtomerAddress cBll = new DataLib.BLL.CumtomerAddress();
            this.dropaddress.DataSource = cBll.GetList(strWhere);
            this.dropaddress.DataValueField = "id";
            this.dropaddress.DataTextField = "address";
            this.dropaddress.DataBind();
        }

        private void getGrid()
        {
            int id = 0;
            int ServerAreaID = 0;
            if (Request["cityid"] != null && Request["cityid"].ToString().Trim() != "")
            {
                id = int.Parse(Request["cityid"].ToString());
            }
            if (Request["ServerAreaID"] != null && Request["ServerAreaID"].ToString().Trim() != "")
            {
                ServerAreaID = int.Parse(Request["ServerAreaID"].ToString());
            }
            string strwhere = "IsDel = 0 and ServerPointID = " + id + " and ServerAreaID = " + ServerAreaID + "";
            DataLib.BLL.ProjectsRelation sBll = new DataLib.BLL.ProjectsRelation();
            DataTable dt = sBll.GetList(strwhere).Tables[0];
            this.rept.DataSource = dt;
            this.rept.DataBind();
        }
        ///<summary>
        /// 绑定修改信息
        /// </summary>
        private void BindUpdateInfo()
        {
            int addressid = Convert.ToInt32(Request.QueryString["addressid"].ToString());
            DataLib.BLL.ProjectsRelation proBll = new DataLib.BLL.ProjectsRelation();
            DataLib.Model.ProjectsRelation promodel = new DataLib.Model.ProjectsRelation();
            promodel = proBll.GetModel(addressid);
            this.dropaddress.SelectedItem.Text = promodel.AddresssName;
            this.Button1.Text = "保存";
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            int ServerAreaID = 0;
            int ServerPointid = 0;
            if (Request["ServerAreaID"] != null && Request["ServerAreaID"].ToString().Trim() != "")
            {
                ServerAreaID = int.Parse(Request["ServerAreaID"].ToString());
            }
            if (Request["cityid"] != null && Request["cityid"].ToString().Trim() != "")
            {
                ServerPointid = int.Parse(Request["cityid"].ToString());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["addressid"]))
            {
                int addressid = Convert.ToInt32(Request.QueryString["addressid"].ToString());
                DataLib.BLL.ProjectsRelation proBll = new DataLib.BLL.ProjectsRelation();
                DataLib.Model.ProjectsRelation promodel = new DataLib.Model.ProjectsRelation();
                promodel = proBll.GetModel(addressid);
                promodel.AddressID = Convert.ToInt32(this.dropaddress.SelectedValue);
                promodel.AddresssName = this.dropaddress.SelectedItem.Text;
                proBll.Update(promodel);
                Response.Redirect("AllocationLookSaleman.aspx?cityid=" + ServerPointid.ToString() + "&ServerAreaID=" + ServerAreaID.ToString());
            }
            else
            {
                DataLib.Model.ServerArea sModel = new DataLib.Model.ServerArea();
                DataLib.BLL.ServerArea sBll = new DataLib.BLL.ServerArea();
                sModel = sBll.GetModel(ServerAreaID);
                DataLib.Model.ServerPoint pModel = new DataLib.Model.ServerPoint();
                DataLib.BLL.ServerPoint pBll = new DataLib.BLL.ServerPoint();
                pModel = pBll.GetModel(ServerPointid);
                DataLib.Model.ProjectsRelation prModel = new DataLib.Model.ProjectsRelation();
                if (sModel != null)
                {
                    prModel.ProjectID = sModel.ProjectID;
                    prModel.ProjectName = sModel.ProjectName;
                    prModel.ProjectNum = sModel.ProjectNum;
                    prModel.AreaName = sModel.AreaName;
                }
                if (pModel != null)
                {
                    prModel.ServerPointName = pModel.ServerPointName;
                }
                prModel.AddressID = int.Parse(this.dropaddress.SelectedValue.ToString());
                prModel.AddresssName = this.dropaddress.SelectedItem.Text.ToString();
                prModel.SalesmanID = 0;
                prModel.SlaesmanUsername = pModel.Cjoperson_show;
                prModel.SalesmanTruename = pModel.Cjoperson_show;
                prModel.ServerAreaID = ServerAreaID;
                prModel.ServerPointID = ServerPointid;
                prModel.CreateTime = DateTime.Now;
                if (Session["UserInfo"] != null)
                {
                    DataLib.Accounts.Bus.User curUser = new DataLib.Accounts.Bus.User();
                    curUser = (DataLib.Accounts.Bus.User)Session["UserInfo"];
                    prModel.CreateMan = curUser.UserName;
                }

                DataLib.BLL.ProjectsRelation prBll = new DataLib.BLL.ProjectsRelation();
                prBll.Add(prModel);
                getGrid();
            }
        }
        /// <summary>
        /// 删除地址事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnDel_Click(object sender, CommandEventArgs e)
        {
            int addressID = Convert.ToInt32(e.CommandName);
            new DataLib.BLL.ProjectsRelation().RelationDelete(addressID, 0, 0, 0, 1);
            getGrid();
        }
    }
}